home *** CD-ROM | disk | FTP | other *** search
/ Aminet 35 / Aminet 35 (2000)(Schatztruhe)[!][Feb 2000].iso / Aminet / game / shoot / ADescentSrc.lha / descent / ui / inputbox.c < prev    next >
C/C++ Source or Header  |  1998-08-08  |  4KB  |  159 lines

  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  12. */
  13.  
  14. #include <stdlib.h>
  15. #include <string.h>
  16.  
  17. #include "mem.h"
  18. #include "fix.h"
  19. #include "types.h"
  20. #include "gr.h"
  21. #include "ui.h"
  22. #include "key.h"
  23.  
  24. // insert character c into string s at position p.
  25. void strcins(char *s, int p, char c)
  26. {
  27.     int n;
  28.     for (n=strlen(s)-p; n>=0; n-- )
  29.         *(s+p+n+1) = *(s+p+n);   // Move everything over
  30.     *(s+p) = c;         // then insert the character
  31. }
  32.  
  33. // delete n character from string s starting at position p
  34.  
  35. void strndel(char *s, int p, int n)
  36. {
  37.     for (; (*(s+p) = *(s+p+n)) != '\0'; s++ )
  38.         *(s+p+n) = '\0';    // Delete and zero fill
  39. }
  40.  
  41. void ui_draw_inputbox( UI_GADGET_INPUTBOX * inputbox )
  42. {
  43.     int w, h, aw;
  44.  
  45.     if ((inputbox->status==1) || (inputbox->position != inputbox->oldposition))
  46.     {
  47.         ui_mouse_hide();
  48.         gr_set_current_canvas( inputbox->canvas );
  49.  
  50.         if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)inputbox)
  51.         {
  52.             if (inputbox->first_time)
  53.                 gr_set_fontcolor( CBLACK, CRED );
  54.             else
  55.                 gr_set_fontcolor( CRED, CBLACK );
  56.         }
  57.         else
  58.             gr_set_fontcolor( CWHITE, CBLACK );
  59.  
  60.         inputbox->status = 0;
  61.  
  62.         gr_string( 2, 2, inputbox->text );
  63.         gr_get_string_size(inputbox->text, &w, &h, &aw  );
  64.  
  65.         gr_setcolor( CBLACK );
  66.         gr_rect( 2+w, 0, inputbox->width-1, inputbox->height-1 );
  67.  
  68.         if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)inputbox  && !inputbox->first_time )
  69.         {
  70.             gr_setcolor(CRED);
  71.             Vline( 2,inputbox->height-3, 2+w+1 );
  72.             Vline( 2,inputbox->height-3, 2+w+2 );
  73.         }
  74.  
  75.         ui_mouse_show();
  76.     }
  77. }
  78.  
  79. UI_GADGET_INPUTBOX * ui_add_gadget_inputbox( UI_WINDOW * wnd, short x, short y, short length, short slength, char * text )
  80. {
  81.     int h, w, aw, f;
  82.     UI_GADGET_INPUTBOX * inputbox;
  83.  
  84.     gr_get_string_size( NULL, &w, &h, &aw );
  85.  
  86.     inputbox = (UI_GADGET_INPUTBOX *)ui_gadget_add( wnd, 6, x, y, x+aw*slength-1, y+h-1+4 );
  87.  
  88.     f = 0;
  89.  
  90.     inputbox->text = malloc( length + 1);
  91.     strncpy( inputbox->text, text, length );
  92.     inputbox->position = strlen(inputbox->text);
  93.     inputbox->oldposition = inputbox->position;
  94.     inputbox->width = aw*slength;
  95.     inputbox->height = h+4;
  96.     inputbox->length = length;
  97.     inputbox->slength = slength;
  98.     inputbox->pressed = 0;
  99.     inputbox->first_time = 1;
  100.  
  101.     gr_set_current_canvas( inputbox->canvas );
  102.     gr_setcolor( CBLACK );
  103.     gr_rect( 0, 0, inputbox->width-1, inputbox->height-1 );
  104.  
  105.     return inputbox;
  106.  
  107. }
  108.  
  109.  
  110. void ui_inputbox_do( UI_GADGET_INPUTBOX * inputbox, int keypress )
  111. {
  112.     ubyte ascii;
  113.     inputbox->oldposition = inputbox->position;
  114.  
  115.     inputbox->pressed=0;
  116.  
  117.     if (CurWindow->keyboard_focus_gadget==(UI_GADGET *)inputbox)
  118.     {
  119.         switch( keypress )
  120.         {
  121.         case 0:
  122.             break;
  123.         case (KEY_LEFT):
  124.         case (KEY_BACKSP):
  125.             if (inputbox->position > 0)
  126.                 inputbox->position--;
  127.             inputbox->text[inputbox->position] = 0;
  128.             inputbox->status = 1;
  129.             if (inputbox->first_time) inputbox->first_time = 0;
  130.             break;
  131.         case (KEY_ENTER):
  132.             inputbox->pressed=1;
  133.             inputbox->status = 1;
  134.             if (inputbox->first_time) inputbox->first_time = 0;
  135.             break;
  136.         default:
  137.             ascii = key_to_ascii(keypress);
  138.             if ((ascii < 255 ) && (inputbox->position < inputbox->length-2))
  139.             {
  140.                 if (inputbox->first_time) {
  141.                     inputbox->first_time = 0;
  142.                     inputbox->position = 0;
  143.                 }
  144.                 inputbox->text[inputbox->position++] = ascii;
  145.                 inputbox->text[inputbox->position] = 0;
  146.             }
  147.             inputbox->status = 1;
  148.             break;
  149.         }
  150.     } else {
  151.         inputbox->first_time = 1;
  152.     }
  153.  
  154.     last_keypress=0;
  155.     
  156.     ui_draw_inputbox( inputbox );
  157.  
  158. }
  159.